package com.garlic.face.jlf.presenter;

import android.text.TextUtils;

import com.alipay.xdevicemanager.api.XDeviceManager;
import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.ServiceInterface;
import com.garlic.face.jlf.transaction.constances.JLFConstant;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.ToastUtils;
import com.garlic.face.jlf.utils.Tools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by shiyanqi on 18/9/18.
 */

public class CardPayPresenter implements CardPayContract.Presenter {
	private CardPayContract.View mView;
	private XDeviceManager mXDeviceManager = null;

	public CardPayPresenter(CardPayContract.View view){
		this.mView = view;
		try {
			mXDeviceManager = new XDeviceManager(App.getContext());
			mXDeviceManager.initContext();
		} catch (Exception e) {
			if (!TextUtils.isEmpty(e.getMessage())) {
				e.printStackTrace();
				LogUtils.e("BASE ACTIVITY " + e.getMessage());
			}
		}
	}

	@Override
	public void cancelPay(String reason) {
		LogUtils.d(reason, false);
		mView.showProgress();
		ServiceInterface.getInstance().cancelTrade("", "");

	}

	@Override
	public void parseJson(JSONObject jsonObject) {
		String interfacetype = "";
		String retflag = "";
		String retmsg = "";
		try {
			interfacetype = jsonObject.getString("INTERFACETYPE");
			retflag = jsonObject.getString("retflag");
			retmsg = jsonObject.getString("retmsg");
			if (interfacetype.equals(JLFConstant.TAG_PAY_RESPONSE)) {
				mView.hideProgress();
				if (retflag.equals("0")) {
					Means.getInstance().setTendCount(Means.getInstance().getTendCount() + 1);
					//TODO:这段代码是最初我们自己打印格式时使用的。。
//					PayFlow payFlow = new PayFlow();
//					payFlow.setAmount(jsonObject.getDouble("amount") + jsonObject.getDouble("remain"));
//					payFlow.setChange(jsonObject.getDouble("change"));
//					payFlow.setFlowid(Means.getInstance().getTendCount());
//					payFlow.setKeyindex("3");
//					payFlow.setPayid(jsonObject.getString("payid"));
//					payFlow.setTrace(jsonObject.getString("trace"));
					if (jsonObject.getString("remain").equals("0.00")) {
						String receiptLines = jsonObject.getString("receiptLines");
						if(!TextUtils.isEmpty(receiptLines)){
							String[] receiptArray = receiptLines.replace("[","").replace("]","").split(",");
							ArrayList<String> receiptList = new ArrayList<String>();
							for(int i = 0; i<receiptArray.length; i++){
								receiptList.add(receiptArray[i]);
							}
							Means.getInstance().setPrintBillHead(receiptList);
						}
//						Means.getInstance().getPayHead().add(payFlow);
						//支付完成
						LogUtils.d("支付完成回到成功界面", true);
						mView.toSuccssActivity("");
					} else {
						// TODO: 2018/7/24 部分付款
//						Means.getInstance().getPayHead().add(payFlow);
						mView.toPartialPaymentActivity(jsonObject.getString("remain"));
					}
				} else {
					String s = String.format(App.getContext().getResources().getString(R.string.pay_error),retmsg);
					ToastUtils.toastShow(s, ToastUtils.ERROR);
//					mView.toFailureActivity(retmsg);
					mView.showProgress();
					ServiceInterface.getInstance().stateLogin();
				}
			} else if (interfacetype.equals(JLFConstant.TAG_CANCELTRADE_RESPONSE)) {
				mView.hideProgress();
				if (retflag.equals("0")) {
					Tools.clearTrade();
					mView.gotoMain();
				} else {
					ToastUtils.toastShow(retmsg, ToastUtils.ERROR);
					LogUtils.e("取消交易" + retmsg);
				}
			} else if(interfacetype.equals(JLFConstant.TAG_STATE_RESPONSE)){
				if (retflag.equals("0")) {
					mView.hideProgress();
					if (retmsg.equals(JLFConstant.POS_PARTIAL_PAYMENT)) {
						mView.toFailureActivity(retmsg, JLFConstant.SCO_PARTIAL_PAY_PSV);
					}else {
						mView.toFailureActivity(retmsg, "");
					}
				}else {
					mView.hideProgress();
					//获取状态失败
//                    ToastUtils.toastShow("获取POS状态失败   需要重启POS", ToastUtils.ERROR);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void destoryMXdevice() {
		try{
			if (mXDeviceManager != null) {
				mXDeviceManager.uninitContext();
				mXDeviceManager = null;
			}
		}catch (Exception e){
			e.printStackTrace();
			LogUtils.e("BASE ACTIVITY " + e.getMessage());
		}
	}

	@Override
	public void payCard() {
		Double d_amount = Means.getInstance().getRec_Tran().getAmount();
		Double d_remain = Means.getInstance().getRec_Tran().getRemian();
		String amount = d_amount+"";
		String ytAmount = amount +"";
		ServiceInterface.getInstance().payTrade(JLFConstant.PAY_COOP_CARD, "", amount, "", "", "1", ytAmount, "");
	}
}
